//
//  ESNewsWordsSearchViewController.m
//  EasyStyle
//
//  Created by FK on 16/5/11.
//  Copyright © 2016年 FK. All rights reserved.
//

#import "ESNewsWordsSearchViewController.h"
#import "UIImageView+WebCache.h"
#import "ESMainNetTaskManager.h"
#import "ESNewsWordsDetailNetResult.h"
#import "ESNewsWordSearchDetailCell.h"
#import "ESWeiXinNewsDetailViewController.h"

@interface ESNewsWordsSearchViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *searchResultList;

@property (nonatomic, strong) UIImageView *headImageView;

@end

@implementation ESNewsWordsSearchViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self startNewsWordNetSearch];
    
    [self setupRootView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark -- 布局相关
- (void)setupRootView
{
    
    self.view.backgroundColor = [UIColor colorWithHex:@"eaeaea"];
    
    WS(weakSelf);
    self.navigationController.navigationBarHidden = YES;
    [self addCustomNavigationBarWithStyle:DefaultStyle customColor:[UIColor colorWithHex:@"3ec5dc"]];
    [self.sgCustomNavigationBar showBackButtonWithActionBlock:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    [self.sgCustomNavigationBar setTitle:_newsWord];
    self.sgCustomNavigationBar.titleLabel.textColor = [UIColor whiteColor];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.sgCustomNavigationBar.height, self.view.width, self.view.height-self.sgCustomNavigationBar.height) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.showsVerticalScrollIndicator = NO;
//    _tableView.separatorInset = UIEdgeInsetsMake(0,13,0,13);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor colorWithHex:@"eaeaea"];
    [self.view addSubview:_tableView];
    
    [self setLayoutHeaderView];
    
}

//添加headerView方法
- (void)setLayoutHeaderView {
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
    
    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
    
    //    self.headImageView.image = [UIImage imageNamed:@"132.jpg"];
    [headView addSubview:self.headImageView];
    self.tableView.tableHeaderView = headView;
    
}


- (void)setHeaderViewImage
{
    for (NewsWordsDetailItem *item in self.searchResultList) {
        if (item.img.length > 0) {
            
            [self.headImageView sd_setImageWithURL:[NSURL URLWithString:item.img] placeholderImage:nil];
            return;
        }
    }
    
    self.tableView.tableHeaderView = nil;
    
}

#pragma mark -- 网络相关
- (void)startNewsWordNetSearch
{
    WS(weakSelf);
    ESMainNetTaskManager *netTask = [ESMainNetTaskManager new];
    
    [netTask setBlockWithReturnBlock:^(id result) {
        /// 这里写刷新view逻辑
        if ([result isKindOfClass:[NSArray class]]) {
            //            _weatherNetResult = [ESWeatherNetResult new];
            
            //            [_weatherNetResult getDetailWeatherData:result andDataType:nil];
            weakSelf.searchResultList = result;
            [weakSelf setHeaderViewImage];
            //            weakSelf.headImageView sd_setImageWithURL:<#(NSURL *)#> placeholderImage:<#(UIImage *)#>
            [weakSelf.tableView reloadData];
            
//            ESLog(@"热词检索返回");
        }
        
    } WithErrorBlock:^(id errorCode) {
        
    } WithFailureBlock:^{
        
    }];
    
    [netTask loadNewsWordSearchDataWithWord:_newsWord];
}



#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat yOffset = scrollView.contentOffset.y  ;
    
    if (yOffset < 0) {
        CGFloat totalOffset = 200 + ABS(yOffset);
        CGFloat f = totalOffset / 200;
        
        _headImageView.frame = CGRectMake(- (width * f - width) / 2, yOffset, width * f, totalOffset);
        
        
    }
    
}


#pragma mark -- UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_searchResultList != nil && _searchResultList.count > 0) {
        return _searchResultList.count;
    } else {
        return 0;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ESNewsWordSearchDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ESNewsWordSearchDetailCell"];
    if (cell == nil) {
        cell = [ESNewsWordSearchDetailCell loadViewFromSameXibName];
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    }
    NewsWordsDetailItem *item = [_searchResultList objectAtIndex:indexPath.row];
    
    cell.searchResult = item;
    //    cell.newsContent.text = item.content;
    //    cell.newsTitle.text = item.title;
    //    cell.newsSrc.text = item.src;
    //    cell.newsPushTime.text = item.pdate;
    //
    return cell;
}


#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsWordsDetailItem *item = [_searchResultList objectAtIndex:indexPath.row];

    return [ESNewsWordSearchDetailCell getLabelRectWithText:item];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NewsWordsDetailItem *item = [_searchResultList objectAtIndex:indexPath.row];
    if (item == nil || item.url == nil || item.url.length <= 0) {
        return;
    }
    
    self.hidesBottomBarWhenPushed = YES;
    ESWeiXinNewsDetailViewController *detailVC = [ESWeiXinNewsDetailViewController new];
    detailVC.newsTitle = item.title;
    detailVC.newsURL = item.url;
    [self.navigationController pushViewController:detailVC animated:YES];
}



@end
